/** 
 * @file xyxkc
 * @brief xyxkc's ESPONE Mind+ library.
 * @n This is a MindPlus graphics programming extension for xyxkc's module.
 * 
 * @copyright    xyxkc,2023
 * @copyright    MIT Lesser General Public License
 * 
 * @author [email](xyxkc@qq.com)
 * @date  2023-04-10
*/
enum HIGHLOW {
    //% block="LOW"
    LOW,
    //% block="HIGH"
    HIGH
}

enum FILL{
    //% block="allBlack"
    0,
    //% block="allWhite"
    1,
}

enum LINE {
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="3"
    3,
    //% block="4"
    4
}

enum BTN {
    //% block="A"
    A,
    //% block="B"
    B,
}
enum  EROTATION {
    //% block="0"
    eROTATION_0,
    //% block="180"
    eROTATION_180,
}
enum ISFILL{
    //% block="fill"
    fillCircle,
    //% block="notFilled"
    drawCircle,
}

enum  ISFILLBOOL{
    //% block="fill"
    true,
    //% block="notFilled"
    false,
}

enum BTN {
    //% block="A"
    A,
    //% block="B"
    B,
    //% block="A+B"
    AB
}
enum BUTTONSTATE {
    //% block="press down"
    Pressed,
    //% block="release"
    UnPressed,
}
enum TOUCHPAD {
    //% block="X1"
    P,
    //% block="Y"
    Y,
    //% block="X2"
    T,
    //% block="E"
    H,
    //% block="D"
    O,
    //% block="U"
    N
}

enum SETTOUCHBUTTONTHRESHOLD {
    //% block="X1"
    P,
    //% block="Y"
    Y,
    //% block="X2"
    T,
    //% block="E"
    H,
    //% block="D"
    O,
    //% block="U"
    N,
    //% block="whole"
    whole
}

enum TOUCHPADSTATE {
    //% block="connect"
    Touched,
    //% block="toBreakOff"
    UnTouched,
}
enum AXIS{
    //% block="X"
    X,
    //% block="Y"
    Y,
    //% block="Z"
    Z,
    //% block="strength"
    Strength
}

enum ATTITUDE{
    //% block="tilt forward"
    TiltForward,
    //% block="tilt backwards"
    TiltBack,
    //% block="tilt to the left"
    TiltLeft,
    //% block="tilt to the right"
    TiltRight,
    //% block="screen up"
    ScreenUp,
    //% block="screen facing downwards"
    ScreenDown,
    //% block="Shake it"
    Shake,

}

enum LAMPNUMBER{
    //% block="0"
    0,
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="All (-1)"
    3
}
enum LAMP{
    //% block="All"
    3,
    //% block="number 0"
    0,
    //% block="number 1"
    1,
    //% block="number 2"
    2

}
enum BRIGHTNESS{
    //% block="9"
    9,
    //% block="8"
    8,
    //% block="7"
    7,
    //% block="6"
    6,
    //% block="5"
    5,
    //% block="4"
    4,
    //% block="3"
    3,
    //% block="2"
    2,
    //% block="1"
    1,
    //% block="0"
    0,

}

enum IDMDIGITAL {
    //% blockId="DIM_BUTTON" block="BUTTON"
    BUTTON,
    //% blockId="DIM_MAGNETIC_SENSOR" block="MAGNETIC_SENSOR"
    MAGNETIC_SENSOR,
    //% blockId="DIM_TILT_SENSOR" block="TILT_SENSOR"
    TILT_SENSOR,
    //% blockId="DIM_INFRARED_PROXIMITY_SENSOR" block="INFRARED_PROXIMITY_SENSOR"
    INFRARED_PROXIMITY_SENSOR,
    //% blockId="DIM_VIBRATION_SENSOR" block="VIBRATION_SENSOR"
    VIBRATION_SENSOR,
    //% blockId="DIM_PYROELECTRIC_SENSOR" block="PYROELECTRIC_SENSOR"
    PYROELECTRIC_SENSOR,
    //% blockId="DIM_TOUCH_SENSOR" block="TOUCH_SENSOR"
    TOUCH_SENSOR,
    //% blockId="DIM_COLLISION_SWITCH" block="COLLISION_SWITCH"
    COLLISION_SWITCH,
    //% blockId="DIM_LIQUID_LEVEL" block="LIQUID_LEVEL"
    LIQUID_LEVEL,
    //% blockId="DIM_PATROL_SINGLE" block="PATROL_SINGLE"
    PATROL_SINGLE,
    //% blockId="DIM_PATROL_LEFT" block="PATROL_LEFT"
    PATROL_LEFT,
    //% blockId="DIM_PATROL_RIGHT" block="PATROL_RIGHT"
    PATROL_RIGHT,
    //% blockId="DIM_ROTARY_A" block="ROTARY_A"
    ROTARY_A,
    //% blockId="DIM_ROTARY_B" block="ROTARY_B"
    ROTARY_B,
    //% blockId="LTCH_LEDBTN" block="Latching LED Button"
    LTCH_LEDBTN,
    //% blockId="MOM_LEDBTN" block="Momentary_LED_Button"
    MOM_LEDBTN,
}

enum IAMANALOG {
    //% blockId="AIM_LIGHT" block="LIGHT"
    LIGHT,
    //% blockId="AIM_SOUND" block="SOUND"
    SOUND,
    //% blockId="AIM_POTENTIOMETER" block="POTENTIOMETER"
    POTENTIOMETER,
    //% blockId="AIM_SOIL_HUMIDITY" block="SOIL_HUMIDITY"
    SOIL_HUMIDITY,
    //% blockId="AIM_ROCKER_X" block="ROCKER_X"
    ROCKER_X,
    //% blockId="AIM_ROCKER_Y" block="ROCKER_Y"
    ROCKER_Y,
    //% blockId="AIM_GRAY" block="GRAY"
    GRAY,
    //% blockId="AIM_MQ_GAS" block="MQ_GAS"
    MQ_GAS,
    //% blockId="AIM_MOISTURE" block="MOISTURE"
    MOISTURE,
    //% blockId="AIM_FLAME" block="FLAME"
    FLAME,
    //% blockId="AIM_MQ_GAS" block="PRESSURE"
    PRESSURE,
}

enum DHT {
    //% blockId="DHT_11" block="DHT11"
    11,
    //% blockId="DHT_22" block="DHT22"
    22,
}

enum DHT_TH {
    //% blockId="DHT_T" block="temperature(℃)"
    Temperature,
    //% blockId="DHT_H" block="humidity(%rh)"
    Humidity,
}

enum SRUNIT {
    //% blockId="SRUNIT_CM" block="cm"
    CM,
    //% blockId="SRUNIT_IN" block="inch"
    IN,
}


enum ODMONOFF {
    //% block="LOW"
    LOW,
    //% block="HIGH"
    HIGH
}
enum OMDDIGITAL {
    //% block="LED"
    LED,
    //% block="Active Buzzer"
    BUZZERACTIVE,
    //% block="Fan"
    FAN,
    //% block="Vibration Motor"
    VIBRATIONMOTOR,
    //% block="Relay"
    RELAY,
    //% block="Pump"
    PUMP
}
enum OMAANALOG {
    //% block="LED"
    LED,
    //% block="Fan"
    FAN,
    //% block="Vibration Motor"
    VIBRATIONMOTOR,
    //% block="Pump"
    PUMP
}

enum TRAFFICLIGHTSTA {
    //% block="All Off"
    0,
    //% block="LED-RED ON"
    1,
    //% block="LED-YELLOW ON"
    2,
    //% block="LED-GREEN ON"
    3
}

enum YFTTS_Parameter {
    //%block="informant"
    m,
    //%block="volume"
    v,
    //%block="speedOfSpeech"
    s,
    //%block="intonation"
    t
}

enum YFTTS_Pronouncer {
    //%block="male voice"
    0,
    //%block="female voice"
    1
}

// 音量，语速，语调
enum YFTTS_VAR {
    //%block="0"
    0,
    //%block="1"
    1,
    //%block="2"
    2,
    //% block="3"
    3,
    //%block="4"
    4,
    //% block="5"
    5,
    //%block="6"
    6,
    //% block="7"
    7,
    //%block="8"
    8,
    //% block="9"
    9
}

enum YFTTS_RING {
    //% block="ring_1"
    ring_1,
    //%block="ring_2"
    ring_2,
    //%block="ring_3"
    ring_3,
    //% block="ring_4"
    ring_4,
    //%block="ring_5"
    ring_5,
    //% block="message_1"
    message_1,
    //%block="message_2"
    message_2,
    //% block="message_3"
    message_3,
    //%block="message_4"
    message_4,
    //% block="message_5"
    message_5,
    //%block="alert_1"
    alert_1,
    //% block="alert_2"
    alert_2,
    //%block="alert_3"
    alert_3,
    //% block="alert_4"
    alert_4,
    //%block="alert_5"
    alert_5
}





//% color="#87c482" iconWidth=50 iconHeight=40
namespace espone {

    //% block="---"
    export function eventProgramNoteSep() {}
    /**
    //% block="▶  eventProgram" blockType="tag"
    export function eventProgram() {}
     **/
    function replace(str :string) {
        return str.replace("+", "");
    }

    //% block="when [BUTTON] button[BUTTONSTATE]" blockType="hat"
    //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.A"
    //% BUTTONSTATE.shadow="dropdown" BUTTONSTATE.options="BUTTONSTATE" BUTTONSTATE.defl="BTN.Pressed"
    export function buttonPress(parameter: any, block: any) {
        let Button = parameter.BUTTON.code;
        let ButtonState = parameter.BUTTONSTATE.code;
        Button = replace(Button);
        let name = 'onButton' + Button + `${ButtonState}`;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addEvent(name, "void", name, "", true);
        Generator.addSetup(`button${Button}.set${ButtonState}Callback`, `button${Button}.set${ButtonState}Callback(${name});`);
    }

    //% block="when touching the button [TOUCHPAD]cover [TOUCHPADSTATE]" blockType="hat"
    //% TOUCHPAD.shadow="dropdown" TOUCHPAD.options="TOUCHPAD" TOUCHPAD.defl="TOUCHPAD.P"
    //% TOUCHPADSTATE.shadow="dropdown" TOUCHPADSTATE.options="TOUCHPADSTATE" TOUCHPADSTATE.defl="TOUCHPADSTATE.Touched"
    export function TopchPress(parameter: any, block: any) {
        let touchButtonNumber = {"P":27,"Y":14,"T":12,"H":13,"O":15,"N":4}
        let touchPad = parameter.TOUCHPAD.code;
        let touchPadState = parameter.TOUCHPADSTATE.code;
        touchPad = replace(touchPad);
        let name = 'pin' + touchButtonNumber[touchPad] + `${touchPadState}Callback`;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addEvent(name, "void", name, "", true);
        Generator.addSetup(`touchPad${touchPad}.set${touchPadState}Callback`, `touchPad${touchPad}.set${touchPadState}Callback(${name});`);
    }

    //% block="when[ATTITUDE]" blockType="hat"
    //% ATTITUDE.shadow="dropdown" ATTITUDE.options="ATTITUDE" ATTITUDE.defl="ATTITUDE.TiltForward"
    export function attitudeEnver(parameter: any, block: any) {
        let attitude = parameter.ATTITUDE.code;
        attitude = replace(attitude);
        let name = `${attitude}Event`;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addEvent(name, "void", name, "", true);
        Generator.addSetup(`accelerometer.onGesture(accelerometer.${attitude},${name})`, `accelerometer.onGesture(accelerometer.${attitude},${name});`);
    }

    //% block="---"
    export function onboardSensorNoteSep() {}
    /**
    //% block="▶  onboardSensor" blockType="tag"
    export function onboardSensor() {}
     **/
    //% block="when [BUTTON] button[BUTTONSTATE]" blockType="boolean"
    //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.A"
    //% BUTTONSTATE.shadow="dropdown" BUTTONSTATE.options="BUTTONSTATE" BUTTONSTATE.defl="BTN.Pressed"
    export function buttonisPress(parameter: any, block: any) {
        let Button = parameter.BUTTON.code;
        let ButtonState = parameter.BUTTONSTATE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        if (ButtonState == "Pressed"){
            Generator.addCode(`(button${Button}.isPressed())`);
        }else{
            Generator.addCode(`(!button${Button}.isPressed())`);
        }
    }
    //% block="touching the button [TOUCHPAD]cover [TOUCHPADSTATE]" blockType="boolean"
    //% TOUCHPAD.shadow="dropdown" TOUCHPAD.options="TOUCHPAD" TOUCHPAD.defl="TOUCHPAD.P"
    //% TOUCHPADSTATE.shadow="dropdown" TOUCHPADSTATE.options="TOUCHPADSTATE" TOUCHPADSTATE.defl="TOUCHPADSTATE.Touched"
    export function touchPadisPress(parameter: any, block: any) {
        let touchPad = parameter.TOUCHPAD.code;
        let touchPadState = parameter.TOUCHPADSTATE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        if (touchPadState == "Touched"){
            Generator.addCode(`(touchPad${touchPad}.isTouched())`);
        }else{
            Generator.addCode(`(!touchPad${touchPad}.isTouched())`);
        }
    }

    //% block="set touch buttons [SETTOUCHBUTTONTHRESHOLD]the touch threshold is[NUMBER]" blockType="command"
    //% SETTOUCHBUTTONTHRESHOLD.shadow="dropdown" SETTOUCHBUTTONTHRESHOLD.options="SETTOUCHBUTTONTHRESHOLD" SETTOUCHBUTTONTHRESHOLD.defl="SETTOUCHBUTTONTHRESHOLD.whole"
    //% NUMBER.shadow="range"   NUMBER.params.min=0    NUMBER.params.max=80    NUMBER.defl=35
    export function setTouchThreshold(parameter: any, block: any) {
        let touchButton = parameter.SETTOUCHBUTTONTHRESHOLD.code;
        let number = parameter.NUMBER.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        if (touchButton == "whole"){
            Generator.addCode(`mPython.setTouchThreshold(${number});`);
        }else{
            Generator.addCode(`touchPad${touchButton}.threshold = ${number};`);
        }
    }
    //% block="read touch buttons[TOUCHPAD]touch value" blockType="reporter"
    //% TOUCHPAD.shadow="dropdown" TOUCHPAD.options="TOUCHPAD" TOUCHPAD.defl="TOUCHPAD.P"
    export function readTouchButtons(parameter: any, block: any) {
        let touchButton = parameter.TOUCHPAD.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`(touchPad${touchButton}.read())`);
    }

    //% block="the current posture is[ATTITUDE]?" blockType="boolean"
    //% ATTITUDE.shadow="dropdown" ATTITUDE.options="ATTITUDE" ATTITUDE.defl="ATTITUDE.TiltForward"
    export function currentPosture(parameter: any, block: any) {
        let currentPosture = parameter.ATTITUDE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`(accelerometer.isGesture(accelerometer.${currentPosture}))`);
    }
    //% block="read microphone sound intensity" blockType="reporter"
    export function soundRead(parameter: any, block: any) {
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`(sound.read())`);
    }

    //% block="acceleration calibration x axis offset[X] y axis offset[Y] z axis offset[Z](m-g)" blockType="command"
    //% X.shadow="range"   X.params.min=-1000    X.params.max=1000    X.defl=0
    //% Y.shadow="range"   Y.params.min=-1000    Y.params.max=1000    Y.defl=0
    //% Z.shadow="range"   Z.params.min=-1000    Z.params.max=1000    Z.defl=0
    export function accelerationCalibration(parameter: any, block: any) {
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let z = parameter.Z.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`accelerometer.setOffset(${x},${y},${z});`);
    }

    //% block="read the value of acceleration(m-g)[AXIS]" blockType="reporter"
    //% AXIS.shadow="dropdown" AXIS.options="AXIS" AXIS.defl="AXIS.X"
    export function accelerometerGet(parameter: any, block: any) {
        let axis = parameter.AXIS.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`(accelerometer.get${axis}())`);
    }


    //% block="---"
    export function oledNoteSep() {}
    /**
    //% block="▶  screenDisplay" blockType="tag"
    export function oledShow() {}
    **/

    //% block="screen display text [TEXT] in the [LINE] thatSOk" blockType="command"
    //% TEXT.shadow="string" TEXT.defl=xyxkc
    //% LINE.shadow="dropdownRound"   LINE.options="LINE"     LINE.defl="LINE.1"
    export function oledprintLine(parameter: any, block: any) {
        let text = parameter.TEXT.code;
        let line = parameter.LINE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.setCursorLine(${line});`);
        Generator.addCode(`display.printLine(${text});`);
    }
    //% block="screen display cleared [LINE] thatSOk" blockType="command"
    //% LINE.shadow="dropdownRound"   LINE.options="LINE"     LINE.defl="LINE.1"
    export function oledfillInLine(parameter: any, block: any) {
        let line = parameter.LINE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.fillInLine(${line},0);`);
    }
    //% block="screen display text [TEXT] at coordinates X:[X] Y:[Y]" blockType="command"
    //% TEXT.shadow="string" TEXT.defl=xyxkc
    //% X.shadow="range"   X.params.min=0    X.params.max=127    X.defl=0
    //% Y.shadow="range"   Y.params.min=0    Y.params.max=63    Y.defl=0
    export function oledprint(parameter: any, block: any) {
        let text = parameter.TEXT.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.setCursor(${x},${y});`);
        Generator.addCode(`display.print(${text});`);
    }
    //% block="the screen displays as [FILL] （clear screen）" blockType="command"
    //% FILL.shadow="dropdown" FILL.options="FILL"  FILL.defl="FILL.0"
    export function fillScreen(parameter: any, block: any) {
        let fill = parameter.FILL.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.fillScreen(${fill});`);
    }
    //% block="rotate the screen to[EROTATION]degree" blockType="command"
    //% EROTATION.shadow="dropdown" EROTATION.options="EROTATION"  EROTATION.defl="EROTATION.eROTATION_180"
    export function setRotaion(parameter: any, block: any) {
        let eROTATION = parameter.EROTATION.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.setRotaion(${eROTATION});`);
    }

    //% block="draw pointsX:[X] Y:[Y]" blockType="command"
    //% X.shadow="range"   X.params.min=0    X.params.max=127    X.defl=0
    //% Y.shadow="range"   Y.params.min=0    Y.params.max=63    Y.defl=0
    export function point(parameter: any, block: any) {
        let x = parameter.X.code;
        let y = parameter.Y.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.point(${x},${y});`);
    }
    //% block="draw the starting point of the lineX1:[X1] Y1:[Y1] end X2:[X2] Y2:[Y2]" blockType="command"
    //% X1.shadow="range"   X1.params.min=0    X1.params.max=127    X1.defl=0
    //% Y1.shadow="range"   Y1.params.min=0    Y1.params.max=63    Y1.defl=0
    //% X2.shadow="range"   X2.params.min=0    X2.params.max=127    X2.defl=0
    //% Y2.shadow="range"   Y2.params.min=0    Y2.params.max=63    Y2.defl=0
    export function line(parameter: any, block: any) {
        let x1 = parameter.X1.code;
        let y1 = parameter.Y1.code;
        let x2 = parameter.X2.code;
        let y2 = parameter.Y2.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.line(${x1},${y1},${x2},${y2});`);
    }

    //% block="draw a circle [ISFILL] center of a circle X:[X] Y:[Y] radius[RADIUS]" blockType="command"
    //% X.shadow="range"   X.params.min=0    X.params.max=127    X.defl=0
    //% Y.shadow="range"   Y.params.min=0    Y.params.max=63    Y.defl=0
    //% RADIUS.shadow="number" RADIUS.defl=20
    //% ISFILL.shadow="dropdown" ISFILL.options="ISFILL"  ISFILL.defl="ISFILL.fillCircle"
    export function Circle(parameter: any, block: any) {
        let isFill = parameter.ISFILL.code;
        let y = parameter.Y.code;
        let x = parameter.X.code;
        let radius = parameter.RADIUS.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.${isFill}(${x},${y},${radius});`);
    }

    //% block="draw a rectangle [ISFILL] starting point X:[X] Y:[Y] wide:[WIDE] high:[HEIGHT]" blockType="command"
    //% X.shadow="range"   X.params.min=0    X.params.max=127    X.defl=0
    //% Y.shadow="range"   Y.params.min=0    Y.params.max=63    Y.defl=0
    //% WIDE.shadow="range"   WIDE.params.min=0    WIDE.params.max=128    WIDE.defl=0
    //% HEIGHT.shadow="range"   HEIGHT.params.min=0    HEIGHT.params.max=64    HEIGHT.defl=0
    //% ISFILL.shadow="dropdown" ISFILL.options="ISFILLBOOL"  ISFILL.defl="ISFILLBOOL.true"
    export function rect(parameter: any, block: any) {
        let isFill = parameter.ISFILL.code;
        let y = parameter.Y.code;
        let x = parameter.X.code;
        let w = parameter.WIDE.code;
        let h = parameter.HEIGHT.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`display.rect(${x},${y},${w},${h},${isFill});`);
    }

    //% block="---"
    export function rgbNoteSep() {}

    //% block="lampNumber[LAMPNUMBER]displayColor[COLOR]" blockType="command"
    //% LAMPNUMBER.shadow="dropdownRound"   LAMPNUMBER.options="LAMPNUMBER"     LAMPNUMBER.defl="LAMPNUMBER.0"
    //% COLOR.shadow="colorPalette"
    export function rgbWrite(parameter: any, block: any) {
        let lampNumber = parameter.LAMPNUMBER.code;
        let color = parameter.COLOR.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        if(lampNumber == "3"){
            Generator.addCode(`rgb.write(-1,${color});`);
        }else{
            Generator.addCode(`rgb.write(${lampNumber},${color});`);
        }

    }

    //% block="close[LAMP]LED" blockType="command"
    //% LAMP.shadow="dropdown"   LAMP.options="LAMP"     LAMP.defl="LAMP.3"
    export function rgbShut(parameter: any, block: any) {
        let lampNumber = parameter.LAMP.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        if(lampNumber == "3"){
            Generator.addCode(`rgb.write(-1,0x000000);`);
        }else{
            Generator.addCode(`rgb.write(${lampNumber},0x000000);`);
        }
    }

    //% block="red[RED]green[GREEN]blue[BLUE]" blockType="reporter"
    //% RED.shadow="range"   RED.params.min=0    RED.params.max=255    RED.defl=255
    //% GREEN.shadow="range"   GREEN.params.min=0    GREEN.params.max=255    GREEN.defl=255
    //% BLUE.shadow="range"   BLUE.params.min=0    BLUE.params.max=255    BLUE.defl=255

    export function rgbThreePrimaryColors(parameter: any, block: any) {
        let red = parameter.RED.code;
        let green = parameter.GREEN.code;
        let blue = parameter.BLUE.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addObject("rgbToColor","uint32_t",`rgbToColor(uint8_t r, uint8_t g, uint8_t b)
        {
         return (uint32_t)((((uint32_t)r<<16) | ((uint32_t)g<<8)) | (uint32_t)b);
        }`)
        Generator.addCode(`rgbToColor(round(${red}), round(${green}), round(${blue}))`);
    }

    //% block="Set the brightness of the LED light to[BRIGHTNESS]" blockType="command"
    //% BRIGHTNESS.shadow="dropdownRound"   BRIGHTNESS.options="BRIGHTNESS"     BRIGHTNESS.defl="BRIGHTNESS.9"

    export function brightness(parameter: any, block: any) {
        let brightness = parameter.BRIGHTNESS.code;

        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`rgb.brightness(round(${brightness}));`);
    }

    //% block="Read the brightness of LED lights" blockType="reporter"

    export function getbrightness(parameter: any, block: any) {

        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addCode(`rgb.brightness()`);
    }

    //% block="---"
    export function NFCNoteSep() {}
    //% block="Look for new card" blockType="boolean"

    export function ScanCard(parameter: any, block: any) {
        Generator.addInclude('XYXKC_ESP32_E.rfid', '#include <MFRC522_I2C.h>');
        Generator.addSetup("XYXKC_ESP32_E.rfid.init", "rfid.PCD_Init();");
        Generator.addCode(`(rfid.PICC_IsNewCardPresent()==MFRC522::STATUS_OK && rfid.PICC_ReadCardSerial()==MFRC522::STATUS_OK)`);
    }

    //% block="Card UID is [UID]" blockType="boolean"
    //% UID.shadow="string" UID.defl="2568daab"
    export function ScanCardUID(parameter: any, block: any) {
        let uid = parameter.UID.code;
        Generator.addInclude('XYXKC_ESP32_E.rfid', '#include <MFRC522_I2C.h>');
        Generator.addSetup("XYXKC_ESP32_E.rfid.init", "rfid.PCD_Init();");
        Generator.addCode(`( rfid.IsCardUid(${uid}) )`);
    }


    //% block="RFID Print UID about the card(String)" blockType="reporter"
    export function PrintUid(parameter: any, block: any) {
        Generator.addInclude('XYXKC_ESP32_E.rfid', '#include <MFRC522_I2C.h>');
        Generator.addSetup("XYXKC_ESP32_E.rfid.init", "rfid.PCD_Init();");
        Generator.addCode(`rfid.readUid()`);
    }

    //% block="---"
    export function TTSNoteSep() {}

    //% block="Set TTS Module [parameter_name] [parameter_val]" blockType="command"
    //% parameter_name.shadow="dropdown" parameter_name.options="YFTTS_Parameter" parameter_name.defl="YFTTS_Parameter.'m'"
    //% parameter_val.shadow="dropdown" parameter_val.options="YFTTS_VAR" parameter_val.defl="YFTTS_VAR.0"
    export function YFTTSSet(parameter: any, block: any) {

        let parameter_name = parameter.parameter_name.code;
        let parameter_val = parameter.parameter_val.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addSetup("XYXKC_ESP32_ETTS.begin", "TTS.begin(115200);");
        Generator.addCode(`TTS.setTTSParameters('${parameter_name}', ${parameter_val});`);

    }

    //% block="TTS Module Speak [String]" blockType="command"
    //% String.shadow="string" String.defl=你好
    export function YFTTSSpeak(parameter: any, block: any) {
        let str = parameter.String.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addSetup("XYXKC_ESP32_ETTS.begin", "TTS.begin(115200);");
        Generator.addCode(`TTS.speak(${str});`);

    }

    //% block="TTS Module Play Ringtone[yftts_ring]" blockType="command"
    //% yftts_ring.shadow="dropdown" yftts_ring.options="YFTTS_RING" yftts_ring.defl="YFTTS_RING.ring_1"
    export function YFTTSRing(parameter: any, block: any) {
        let yftts_ring = parameter.yftts_ring.code;
        Generator.addInclude('XYXKC_ESP32_E', '#include <XYXKC_ESP32_E.h>');
        Generator.addSetupMainTop("XYXKC_ESP32_E.begin", "mPython.begin();");
        Generator.addSetup("XYXKC_ESP32_ETTS.begin", "TTS.begin(115200);");
        Generator.addCode(`TTS.speak("${yftts_ring}");`);
    }


    //% block="---"
    export function externalSensorsNoteSep() {}

    //% block="read [INPUTMODULEDIGITAL] on [IDMPIN]" blockType="boolean"
    //% INPUTMODULEDIGITAL.shadow="dropdown" INPUTMODULEDIGITAL.options="IDMDIGITAL" INPUTMODULEDIGITAL.defl="IDMDIGITAL.MAGNETIC_SENSOR"
    //% IDMPIN.shadow="dropdown" IDMPIN.options="PIN_DigitalRead"
    export function inputDigitalModule(parameter: any, block: any) {
        let inputModule = parameter.INPUTMODULEDIGITAL.code;
        let inputModulePin = parameter.IDMPIN.code;

        if(Generator.board === 'pico'){//如果是pico板，生成如下代码
            Generator.addSetup(`pinMode_${inputModulePin}`,`pinMode(${inputModulePin}, INPUT);`);
        }

        Generator.addCode(`digitalRead(${inputModulePin})`);
    }

    //% block="read [INPUTMODULEANALOG] on [IAMPIN]" blockType="reporter"
    //% INPUTMODULEANALOG.shadow="dropdown" INPUTMODULEANALOG.options="IAMANALOG" INPUTMODULEANALOG.defl="IAMANALOG.LIGHT"
    //% IAMPIN.shadow="dropdown" IAMPIN.options="PIN_AnalogRead"
    export function inputAnalogModule(parameter: any, block: any) {
        let inputModule = parameter.INPUTMODULEANALOG.code;
        let inputModulePin = parameter.IAMPIN.code;
        Generator.addCode(`analogRead(${inputModulePin})`);
    }



    //% block="read T&H on [IAMPIN] [DHTMODULE] [TH]" blockType="reporter"
    //% IAMPIN.shadow="dropdown" IAMPIN.options="PIN_DigitalWrite"
    //% DHTMODULE.shadow="dropdown" DHTMODULE.options="DHT" DHTMODULE.defl="DHT.11"
    //% TH.shadow="dropdown" TH.options="DHT_TH" TH.defl="DHT_TH.Temperature"
    export function readdhtSensor(parameter: any, block: any) {
        let inputModulePin = parameter.IAMPIN.code;
        let dht = parameter.DHTMODULE.code;
        let dht_th = parameter.TH.code;
        Generator.addInclude("include_DFRobot_DHT", `#include <DFRobot_DHT.h>`);
        Generator.addObject("object_DFRobot_DHT", `DFRobot_DHT`, `dht${dht}_${inputModulePin};`);
        Generator.addSetup(`initSetup_DFRobot_DHT`, `dht${dht}_${inputModulePin}.begin(${inputModulePin},DHT${dht});`);
        Generator.addCode(`dht${dht}_${inputModulePin}.get${dht_th}()`);
    }

    //% block="read ulrasonic sensor Unit [UNIT] trig[TRIGPIN] echo[ECHOPIN]" blockType="reporter"
    //% UNIT.shadow="dropdown" UNIT.options="SRUNIT" UNIT.defl="SRUNIT.CM"
    //% TRIGPIN.shadow="dropdown" TRIGPIN.options="PIN_DigitalRead"
    //% ECHOPIN.shadow="dropdown" ECHOPIN.options="PIN_DigitalRead"
    export function readUlrasonicSensor(parameter: any, block: any) {
        let unit = parameter.UNIT.code;
        let trigpin = parameter.TRIGPIN.code;
        let echopin = parameter.ECHOPIN.code;
        Generator.addInclude("include_DFRobot_URM10", `#include <DFRobot_URM10.h>`);
        Generator.addObject("object_DFRobot_URM10", `DFRobot_URM10`, `sr04;`);
        Generator.addCode(`sr04.getDistance${unit}(${trigpin},${echopin})`);
    }
    //% block="read pins[PIN1]infrared reception value" blockType="reporter"
    //% PIN1.shadow="dropdown" PIN1.options="PIN_DigitalWrite"
    export function DFRobot_IRremote(parameter: any, block: any) {
        let pin1 = parameter.PIN1.code;

        Generator.addInclude("include_DFRobot_IRremote", `#include <DFRobot_IRremote.h>`);
        Generator.addObject("object_DFRobot_IRremote", `IRremote_Receive`, `remoteReceive_${pin1};`);
        Generator.addSetup(`DFRobot_IRremote_Setup_${pin1}`,`remoteReceive_${pin1}.begin(${pin1});`);
        Generator.addCode(`(remoteReceive_${pin1}.getIrCommand())`);
    }

    //% block="---"
    export function actuatorNoteSep() {}


    //% block="set [OUTPUTMODULEDIGITAL] on [ODMPIN] output [ODMSTATE]" blockType="command"
    //% OUTPUTMODULEDIGITAL.shadow="dropdown" OUTPUTMODULEDIGITAL.options="OMDDIGITAL" OUTPUTMODULEDIGITAL.defl="OMDDIGITAL.LED"
    //% ODMPIN.shadow="dropdown" ODMPIN.options="PIN_DigitalWrite"
    //% ODMSTATE.shadow="dropdown" ODMSTATE.options="ODMONOFF" ODMSTATE.defl="HIGH"
    export function outputiDigitalModule(parameter: any, block: any) {
        let outputModule = parameter.OUTPUTMODULEDIGITAL.code;
        let outputModulePin = parameter.ODMPIN.code;
        let outputModuleState = parameter.ODMSTATE.code;
        if(Generator.board === 'pico'){//如果是pico板，生成如下代码
            Generator.addSetup(`pinMode_${outputModulePin}`,`pinMode(${outputModulePin}, OUTPUT);`);
        }
        Generator.addCode(`digitalWrite(${outputModulePin},${outputModuleState});`);
    }

    //% block="set [OUTPUTMODULEANALOG] on [OAMPIN] output [OAMSTATE]" blockType="command"
    //% OUTPUTMODULEANALOG.shadow="dropdown" OUTPUTMODULEANALOG.options="OMAANALOG" OUTPUTMODULEANALOG.defl="OMAANALOG.LED"
    //% OAMPIN.shadow="dropdown" OAMPIN.options="PIN_AnalogWrite"
    //% OAMSTATE.shadow="range"   OAMSTATE.params.min=0    OAMSTATE.params.max=255    OAMSTATE.defl=200
    export function outputAnalogModule(parameter: any, block: any) {
        let outputModule = parameter.OUTPUTMODULEANALOG.code;
        let outputModulePin = parameter.OAMPIN.code;
        let outputModuleState = parameter.OAMSTATE.code;
        if(Generator.board === 'esp32'||Generator.board === 'firebeetleesp32'||Generator.board === 'firebeetleesp32e'){//如果是ESP32系列，生成如下代码
            Generator.addCode(`analogWrite(${outputModulePin},map(${outputModuleState}, 0, 255, 0, 1023));`);
        }else{
            Generator.addCode(`analogWrite(${outputModulePin},${outputModuleState});`);
        }
    }

    //% block="traffic light module PIN1 [PIN1] PIN2 [PIN2] [STATE]" blockType="command"
    //% PIN1.shadow="dropdown" PIN1.options="PIN_DigitalWrite"
    //% PIN2.shadow="dropdown" PIN2.options="PIN_DigitalWrite"
    //% STATE.shadow="dropdown" STATE.options="TRAFFICLIGHTSTA" PIN1STATE.defl="TRAFFICLIGHTSTA.1"
    export function trafficLight(parameter: any, block: any) {
        let trafficLightPin1 = parameter.PIN1.code;
        let trafficLightPin2 = parameter.PIN2.code;
        let trafficLightState1 = parameter.STATE.code;

        if(Generator.board === 'pico'){//如果是pico板，生成如下代码
            Generator.addSetup(`pinMode_${trafficLightPin1}`,`pinMode(${trafficLightPin1}, OUTPUT);`);
            Generator.addSetup(`pinMode_${trafficLightPin2}`,`pinMode(${trafficLightPin2}, OUTPUT);`);
        }

        if (trafficLightState1 === `0`){
            Generator.addCode(`digitalWrite(${trafficLightPin1},LOW);`);
            Generator.addCode(`digitalWrite(${trafficLightPin2},LOW);`);
        }else if (trafficLightState1 === `1`){
            Generator.addCode(`digitalWrite(${trafficLightPin1},LOW);`);
            Generator.addCode(`digitalWrite(${trafficLightPin2},HIGH);`);
        }else if (trafficLightState1 === `2`){
            Generator.addCode(`digitalWrite(${trafficLightPin1},HIGH);`);
            Generator.addCode(`digitalWrite(${trafficLightPin2},LOW);`);
        }else if (trafficLightState1 === `3`){
            Generator.addCode(`digitalWrite(${trafficLightPin1},HIGH);`);
            Generator.addCode(`digitalWrite(${trafficLightPin2},HIGH);`);
        }
    }


    //% block="set up[PIN1]pin servo servos are[ANGLE]" blockType="command"
    //% PIN1.shadow="dropdown" PIN1.options="PIN_DigitalWrite"
    //% ANGLE.shadow="range"   ANGLE.params.min=0    ANGLE.params.max=180    ANGLE.defl=0

    export function DFRobot_Servo(parameter: any, block: any) {
        let pin1 = parameter.PIN1.code;
        let angle = parameter.ANGLE.code;
        Generator.addInclude('DFRobot_Servo', '#include <DFRobot_Servo.h>');
        Generator.addObject(`DFRobot_Servo_Object_${pin1}`,"Servo",`servo_${pin1}`);
        Generator.addSetup(`DFRobot_Servo_Setup_${pin1}`,`servo_${pin1}.attach(${pin1});`);
        Generator.addCode(`servo_${pin1}.angle(abs(${angle}));`);

    }



}
